package servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bean.Cart;
import dao.Cartdao;
import sun.nio.cs.US_ASCII;

@WebServlet("/deleteOneCart")
public class deleteOneCart extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cartdao cartdao = new Cartdao();
        response.setCharacterEncoding("utf-8");
        String isbn = request.getParameter("id");
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("name");

        Cart cart = cartdao.findById(username, isbn);
        int num = cart.getNum();
        if(num==1) {
            cartdao.delete(username, isbn);
        }
        else {
            num--;
            cartdao.update(username, isbn, num);
        }

        request.getRequestDispatcher("cartlist.jsp").forward(request,response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
